package ngat.oss;

import ngat.message.OSS.*;
import ngat.util.*;
import ngat.util.logging.*;
import ngat.phase2.*; 
import ngat.instrument.*;
import ngat.phase2.nonpersist.*;

import java.io.*;
import java.lang.reflect.*;

import jyd.dbms.*;
import jyd.storable.*;
import jyd.collection.*;

/**
 * Action: Removes a Proposal Stub. 
 * @author S.N.Fraser
 * $Id: REMOVE_PROPOSALImpl.java,v 1.1 2006/11/27 08:45:00 snf Exp $
 */

public class REMOVE_PROPOSALImpl extends TransactionImpl implements Logging {
    
    /** The path used to locate the Proposal in the OSS database. */
    protected Path     proposalPath;
   
    public REMOVE_PROPOSALImpl(Path proposalPath) {
	super();
	this.proposalPath     = proposalPath;
    }
    
    public REMOVE_PROPOSALImpl(REMOVE_PROPOSAL request) {
	super(request);
	this.proposalPath     = request.getProposalPath();	
    }
    
    /**
     * Places the stub in the DB and updates the OSS password table..
     * @return Boolean or an OSS_Error.
     */
    public TRANSACTION_DONE exec() { 

	Logger logger = LogManager.getLogger("TRACE");
	
	
	User user = null;
	
	Transaction t = null;
	try {
	    t = Transaction.current();
	  
	    // Locate the Proposal's owner (PI) from its path expression.
	    user = OSS_Utilities.fetchUser(proposalPath);
	   
	    // Check the user exists.
	    if (user == null) {
		logger.log(ERROR, 3, "REMOVEPROPOSAL_impl", id, "exec", 
			   "User for: "+proposalPath.toString()+" Not in database.");		
		return error(TRANSACTION.PATH_RESOLUTION_ERROR,
				     "Could not find PI user for: "
				     +proposalPath.toString());
	    }

	    String proposalId = proposalPath.getProposalByName();
	    
	    // Remove
	    user.deleteProposal(proposalId);

	} finally {
	    t.commit();
	}

	REMOVE_PROPOSAL_DONE done = new REMOVE_PROPOSAL_DONE(id);
	done.setSuccessful(true);
	return done;

    }
    
}

/** $Log: REMOVE_PROPOSALImpl.java,v $
/** Revision 1.1  2006/11/27 08:45:00  snf
/** Initial revision
/**
/** Revision 1.1  2006/11/27 08:37:05  snf
/** Initial revision
/** */
